package com.aaa.springboot_rbac.controller;

import com.aaa.springboot_rbac.entity.Dept;
import com.aaa.springboot_rbac.service.DeptService;
import com.aaa.springboot_rbac.util.Page;
import com.aaa.springboot_rbac.util.Result;
import com.aaa.springboot_rbac.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @FileName: DeptController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/8/21 14:30
 * @Version: 1.0.0
 */
@RestController //  相当于是    @Controller 和  @ResponseBody
@RequestMapping("dept")
@Log4j2
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 分页查询
     * @param page
     * @return
     */
   // @GetMapping("queryByPage")
    @PostMapping("queryByPage")
    //@ResponseBody //把返回对象转换成json字符串
    public Result queryByPage(@RequestBody Page<Dept> page) {
/*        PageInfo pageInfo = null;
        try {
            pageInfo = deptService.queryByPage(page);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.failed(null);*/

        log.info("开始执行分页查询");
        PageInfo pageInfo = deptService.queryByPage(page);
        log.info("分页查询结束");
        return ResultUtil.success(pageInfo);
    }


    /**
     * 根据id查询
     * @param deptNo
     * @return
     */
    @GetMapping("queryById")
   // @ResponseBody
    public Result queryById(Integer deptNo) {
        /*Dept dept = null;
        try {
            dept = deptService.queryById(deptNo);
            return ResultUtil.success(dept);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return ResultUtil.failed(null);*/
        return ResultUtil.success(deptService.queryById(deptNo));
    }

    /**
     * 添加
     * @param dept
     * @return
     */
    @PostMapping("add")
   // @ResponseBody
    public Result add(@RequestBody Dept dept) {
        /*int i = 0;
        try {
            i = deptService.add(dept);
            return  ResultUtil.success(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.failed(i);*/
        return ResultUtil.success(deptService.add(dept));
    }

    /**
     * 更新
     * @param dept
     * @return
     */
    @PostMapping("update")
   // @ResponseBody
    public Result update(@RequestBody Dept dept) {
        /*int i = 0;
        try {
            i = deptService.update(dept);
            return  ResultUtil.success(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.failed(0);*/
        return ResultUtil.success(deptService.update(dept));
    }

    /**
     * 根据id删除
     * @param deptNo
     * @return
     */
    @DeleteMapping("deleteById")
    //@ResponseBody
    public Result deleteById(Integer deptNo) {
/*        int i = 0;
        try {
            i = deptService.deleteById(deptNo);
            return  ResultUtil.success(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return ResultUtil.failed(i);*/
        return ResultUtil.success(deptService.deleteById(deptNo));
    }
}
